<?php
require_once('../includes/SmartyManager.class.php');
require_once('../includes/DB.class.php');
require_once('../includes/exceptions'.DS.'AppExceptions.class.php');
require_once('../includes/LogInserter.class.php');
require_once('../includes/SimpleTableExporter.class.php');

$smarty = SmartyManager::getSmarty();
$exports = array();
foreach($_POST as $key => $val){
	if(stristr($key, "export")){
		array_push($exports, "fileId = $val");
	}
}

if(!empty($exports)){
	try{
		$filename = '证书出库明细汇总_'.date('YmdHis').'.xls';
		$exp = new SimpleTableExporter($filename);
		$exportStats = join(' or ', $exports);
		$query = "select * from little_chuku where $exportStats";
		DB::query($query);
		$result = DB::getResult();
		
		$filenames = array();
		$certificateNumbers = array();
		$certificationCodifications = array();
		$chukuDates = array();
		$consignmentBatchs = array();
		$handlers = array();
		$remark1 = array();
		$remark2 = array();
		
		while($row = $result->fetch_assoc()){
			array_push ( $chukuDates, $row ['chukuDate'] );
			array_push ( $certificateNumbers, $row['certificationNumber']);
			array_push ( $certificationCodifications, $row ['certificationCodification'] );
			array_push ( $consignmentBatchs, $row ['consignmentBatch'] );
			array_push ( $handlers, $row ['handler'] );
			array_push ( $remark1, $row ['remark1'] );
			array_push ( $remark2, $row ['remark2'] );
			$query_filename = "select filename from little_chuku_file where id = {$row['fileId']}";
			DB::query($query_filename);
			$result_filename = DB::getResult();
			$row_filename = $result_filename->fetch_assoc();
			array_push ( $filenames, $row_filename['filename']);
		}
		
		$exp->insertCustomData(array($chukuDates,$certificateNumbers,$certificationCodifications,$consignmentBatchs,$handlers,$remark1,$remark2,$filenames), 
				   array('出库日期','产品质量证明书号','产品质量证明书编号','发货批次','经手人','备注一','备注二','出库文件名'), 
						'sheet1');
		$exp->export();
		
	}catch(Exception $e){
		$smarty->assign("errTitle", '导出出库文件失败');
		$smarty->assign('errorType', 'chukuDelete');
		$smarty->assign('errMsg', $e);
		$smarty->display('error.html');
		die();
	}
}

?>